package com.woniu.p2p.behindController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.UserChargeService;
import com.woniu.p2p.entity.User;
import com.woniu.p2p.entity.UserCharge;
import com.woniu.p2p.listener.InChargeListener;
import com.woniu.p2p.vo.ImportCharge;
import com.woniu.p2p.vo.UserAndUserChargeVo;
import com.woniu.p2p.vo.UserChargeRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户充值管理
 */
@Controller
@RequestMapping("/charge")
public class UserChargeController {

    @Autowired
    UserChargeService userChargeService;

    @RequestMapping("/getUserCharge")
    public String getUserCharge(Map map, String chargetype,@DateTimeFormat(pattern = "yyyy-MM-dd") Date predate,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") Date nextdate,
                                String chargekeyword,
                                Integer pagenum,
                                Integer pagesize){
        Integer start=pagenum==null?1:pagenum;
        Integer num=pagesize==null?3:pagesize;
        PageInfo<UserChargeRecordVo> pageInfo = userChargeService.findAllUserChargeByDateAndTypeAndKeyword(predate, nextdate, chargetype, chargekeyword, start, num);
        map.put("usercharge",pageInfo);
        map.put("predate",predate);
        map.put("nextdate",nextdate);
        map.put("chargetype",chargetype);
        map.put("chargekeyword",chargekeyword);
        map.put("pagesize",pagesize);
        return "content/chargeRecord";
    }


    @RequestMapping("/getUser")
    @ResponseBody
    public UserAndUserChargeVo getUser(Map map,Integer id){
        UserCharge allUserCharge = userChargeService.findAllUserCharge(id);
        String chargeOrder = allUserCharge.getChargeOrder();
        String chargeContext = allUserCharge.getChargeContext();
        Long chargeMoney = allUserCharge.getChargeMoney();
        String chargeStatus = allUserCharge.getChargeStatus();
        Date chargeTime = allUserCharge.getChargeTime();
        String chargeType = allUserCharge.getChargeType();

        Integer userid = allUserCharge.getUserid();
        User allUserById = userChargeService.findAllUserById(userid);
        String userHead = allUserById.getUserHead();
        String userEmail = allUserById.getUserEmail();
        Integer userCode = allUserById.getUserCode();
        Long userAmount = allUserById.getUserAmount();
        String userNickname = allUserById.getUserNickname();
        String userRealname = allUserById.getUserRealname();
        System.out.println(allUserCharge);
        System.out.println(allUserById);
        UserAndUserChargeVo userAndUserChargeVo = new UserAndUserChargeVo(userHead,userNickname,userEmail,userAmount,userRealname,
                userCode,chargeOrder,chargeTime, chargeMoney,chargeType,chargeStatus,chargeContext);
        System.out.println(userAndUserChargeVo);
        return userAndUserChargeVo;
    }


    @RequestMapping("/insert")
    @ResponseBody
    public int insert(int[] id, HttpServletRequest request){
        System.out.println(Arrays.toString(id));

        return 1;
    }


    /**
     * 导出
     */
    @RequestMapping("/outCharge")
    public void outCharge(int[] id,
                        HttpServletResponse response) throws IOException {
        System.out.println(Arrays.toString(id));
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试","UTF-8");
        response.setHeader("Content-Dispostion","attachment;filename*=UTF-8''"+fileName+".xlsx");

        List<UserChargeRecordVo> list = userChargeService.findAllUserChaege(id);
        ExcelWriterBuilder write = EasyExcel.write(outputStream, UserChargeRecordVo.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        sheet.doWrite(list);
        outputStream.close();
    }


    @Autowired
    InChargeListener inChargeListener;

    /**
     * 导入
     */
    @RequestMapping("/inCharge")
    @ResponseBody
    public Integer inCharge(@RequestParam("file") MultipartFile file){
        try {
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), ImportCharge.class,inChargeListener);
            ExcelReaderSheetBuilder sheet = read.sheet();
            sheet.doRead();

        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }
}
